package chapter_1;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 根据给定的主机名（host),获取对应的ip地址
 * @author fulv
 */
public class GetIpAddressExample {
    public static void main(String[] args) {

        //输入主机名或者ip地址
        Scanner sc = new Scanner(System.in);
        String line = "";
        while(sc.hasNextLine()){
            //读取输入的主机名或者ip地址，解析对应的ip地址
            line = sc.nextLine();
            System.out.println("输入的host: "+line);
            try {
                InetAddress[] addressList = InetAddress.getAllByName(line);
                for(InetAddress address:addressList){
                    //会利用DNS域名解析来获得主机名对应的ip地址
                    System.out.println("主机名: "+address.getHostName()+",ip地址: "+address.getHostAddress());
                }
                try {
                    boolean status = InetAddress.getByName(line).isReachable(1000);
                    System.out.println("是否能够ping通地址: " + status);
                }catch(IOException e){
                    e.printStackTrace();
                }
            }catch(UnknownHostException e){
                e.printStackTrace();
            }
        }
        sc.close();
    }
}
